package action;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.springframework.beans.factory.annotation.Autowired;
import pageModel.Json;
import pageModel.Role;
import service.RoleServiceI;

import com.opensymphony.xwork2.ModelDriven;
@Namespace("/")
@Action(value="roleAction")
public class RoleAction extends BaseAction implements ModelDriven<Role>{
private RoleServiceI roleService;
Role role =new Role();
	@Override
	public Role getModel() {
		return role;
	}
	public RoleServiceI getRoleService() {
		return roleService;
	}
	@Autowired
	public void setRoleService(RoleServiceI roleService) {
		this.roleService = roleService;
	}
	public void datagrid(){
		super.writeJson(roleService.datagrid(role));
	}
	public void add(){
		Json j=new Json();
		try{
			roleService.save(role);
			j.setSuccess(true);
			j.setMsg("添加成功!");
		}catch(Exception e){
			j.setMsg("添加失败,该角色在数据库中已存在!");
		}
		super.writeJson(j);
	}
	public void del(){
		Json j=new Json();
		try{
		roleService.remove(role.getIds());
		j.setSuccess(true);
		j.setMsg("删除成功!");
	  }catch(Exception e){
		j.setMsg("删除失败!"); 
	  }
		super.writeJson(j);
	}
	public void editor(){
		Json j=new Json();
		try{
		roleService.edit(role);
		j.setSuccess(true);
		j.setMsg("编辑成功!");
	  }catch(Exception e){
		j.setMsg("编辑失败!"); 
	  }
		super.writeJson(j);
	}
	public void getRoleList() {
		super.writeJson(roleService.combobox());
	}
	public void grant(){
		Json j = new Json();
		roleService.grant(role.getId(), role.getIds());
		j.setSuccess(true);
		j.setMsg("授权成功!");
		super.writeJson(j);
	}
}
